﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;
using Workflows.Components.TextMining.Properties;

namespace Workflows.Components.TextMining.Clustering
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class FeatureSetComponent:WorkflowComponentBase
    {
        #region props
        /// <summary></summary>
        public string DocumentTermListFilePath
        {
            get
            {
                return this._Instruction.DocumentTermListFilePath;
            }
            set
            {
                this._Instruction.DocumentTermListFilePath = value;
            }
        }

        /// <summary></summary>
        public string StartingClusterResultFilePath
        {
            get
            {
                return this._Instruction.StartingClusterResultFilePath;
            }
            set
            {
                this._Instruction.StartingClusterResultFilePath = value;
            }
        }

        /// <summary>
        /// i.e. Hierarchical_Cluster_Result_{0}.xml =>
        /// with iteration # 23, it's 
        /// Hierarchical_Cluster_Result_23.xml
        /// 
        /// if {0} is not present, the cluster result will be 
        /// overwritten during each iteration
        /// </summary>
        public string ClusterResultFileNamePattern
        {
            get
            {
                return this._Instruction.ClusterResultFileNamePattern;
            }
            set
            {
                this._Instruction.ClusterResultFileNamePattern = value;
            }
        }

        /// <summary>
        /// this is xml file with the following schema
        /// <Terms>
        ///     <Term TermID="0" Phrase="cardiac" IsFrozen="True" CurrentWeight="2">
        ///         <Iteration Number="1" Weight="1" ClassifierScore="0.9"/>
        ///         <Iteration Number="2" Weight="2" ClassifierScore="1.8"/>
        ///         <Iteration Number="3" Weight="2" ClassifierScore="3.5"/>
        ///     </Term>
        ///     <Term TermID="1" Phrase="cancer disease" IsFrozen="False" CurrentWeight="1.6">
        ///         <Iteration Number="1" Weight="1" ClassifierScore="3"/>
        ///         <Iteration Number="2" Weight="2" ClassifierScore="1.8"/>
        ///         <Iteration Number="3" Weight="1.6" ClassifierScore="3.5"/>
        ///     </Term>
        /// </Terms>
        /// </summary>
        public string TermWeightFilePath
        {
            get
            {
                return this._Instruction.TermWeightFilePath;
            }
            set
            {
                this._Instruction.TermWeightFilePath = value;
            }
        }

        /// <summary>
        /// this is summary of all iterations
        /// <Iterations StartingPerformanceScore="99.23" EndingPerformanceScore="450" StartingClusterResultFilePath="">
        ///     <Iteration Number="1" OverallPerformanceScore="129.75" ClusterResultFilePath=""/>
        /// </Iterations>
        /// </summary>
        public string IterationFilePath
        {
            get
            {
                return this._Instruction.IterationFilePath;
            }
            set
            {
                this._Instruction.IterationFilePath = value;
            }
        }

        private FeatureSetInstruction _Instruction;
        public override InstructionBase Instruction
        {
            get { return _Instruction; }
            set { _Instruction =(FeatureSetInstruction) value; }
        }

        private FeatureSetOutput _Output;
        public override ResultBase Output
        {
            get { return _Output; }
            set { _Output = (FeatureSetOutput) value; }
        }
        #endregion

        #region ctor
        /// <summary>
        /// 
        /// </summary>
        public FeatureSetComponent():base()
        {
            this.ComponentActivityType = WorkflowActivityType.SimpleActivity;
            this.ComponentCategory = WorkflowComponentCategory.TextMining.ToString();
            this.ComponentIcon = Resources.FXSRES_5034;
            this.ComponentImage = Resources.Configurator;
            this.ComponentName = "Term weights";
            this.Description = "Adjust term weights based on cluster tree topology";

            this._Instruction=new FeatureSetInstruction(this.ComponentID,Guid.Empty,
                string.Empty,string.Empty,string.Empty,string.Empty,string.Empty);
            this._Output = new FeatureSetOutput(this.ComponentID, Guid.Empty,
                                                0, 0, 1, 1, 1);
        }
        #endregion

        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = base.Serialize(ref xDoc, ref parentNode);

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "DocumentTermListFilePath", this.DocumentTermListFilePath);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "StartingClusterResultFilePath",
                                        this.StartingClusterResultFilePath);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "TermWeightFilePath", this.TermWeightFilePath);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "IterationFilePath", this.IterationFilePath);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ClusterResultFileNamePattern",
                                        this.ClusterResultFileNamePattern);

            return xNode;
        }

        public override WorkflowComponentBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                FeatureSetComponent wfComponent = base.Instantiate(xNode) as FeatureSetComponent;

                wfComponent.DocumentTermListFilePath =
                    XmlDataUtil.GetAttributeValue(xNode, "DocumentTermListFilePath",
                                                  "");
                wfComponent.StartingClusterResultFilePath =
                    XmlDataUtil.GetAttributeValue(xNode,
                                                  "StartingClusterResultFilePath",
                                                  "");
                wfComponent.TermWeightFilePath =
                    XmlDataUtil.GetAttributeValue(xNode, "TermWeightFilePath", "");
                wfComponent.IterationFilePath =
                    XmlDataUtil.GetAttributeValue(xNode, "IterationFilePath", "");
                wfComponent.ClusterResultFileNamePattern =
                    XmlDataUtil.GetAttributeValue(xNode,
                                                  "ClusterResultFileNamePattern",
                                                  "");

                return wfComponent;
            }
            else
            {
                throw new Exception("Unable to instantiate component: invalid type specified in xml node");
            }
        }
    }
}
